import React, {useRef} from 'react';
import {PageContainer} from '@ant-design/pro-layout';
import ProTable, {ActionType} from '@ant-design/pro-table';
import type {TableListItem} from './data.d';
import {queryType} from "./data.d";
import {queryOrderStockList} from './service';
import {queryUse} from "@/utils/service";
import {columnsSorter, empty} from "@/utils/function";
import {Card} from "antd";


const StockList: React.FC = () => {
  /** 新建窗口的弹窗 */
  const columns: any[] = [
    {
      title: "订单ID",
      dataIndex: 'orderId',
      key: 'orderId',
      fixed: "left",
      width: 100,
      // filters: true,
      // onFilter: true,
      tip: '订单ID是唯一的 key',
    },
    {
      title: "交易对",
      search: false,
      dataIndex: 'symbol',
      key: 'symbol',
      fixed: "left",
      width: 90,
    },
    {
      title: "原始订单数量",
      search: false,
      width: 80,
      dataIndex: 'origQty',
      key: 'origQty',
    },
    {
      title: "交易的订单数量",
      search: false,
      width: 80,
      dataIndex: 'executedQty',
      key: 'executedQty',
    },
    {
      title: "累计交易的金额",
      search: false,
      width: 100,
      dataIndex: 'cummulativeQuoteQty',
      key: 'cummulativeQuoteQty',
    },
    {
      title: "订单状态",
      search: false,
      width: 80,
      dataIndex: 'status',
      key: 'status',
    },
    {
      title: "订单的时效方式",
      search: false,
      width: 100,
      dataIndex: 'timeInForce',
      key: 'timeInForce',
    },
    {
      title: "订单类型",
      search: false,
      width: 80,
      dataIndex: 'type',
      key: 'type',
    },
    {
      title: "买卖方向",
      search: false,
      width: 80,
      dataIndex: 'side',
      key: 'side',
    },
  ];
  const actionRef = useRef<ActionType>();
  columnsSorter("ContractList现货下单记录列表", columns);
  return (
    <PageContainer>
      <Card>
      <ProTable<TableListItem>
        headerTitle={"现货下单记录列表"}
        rowKey="key"
        bordered
        actionRef={actionRef}
        search={{
          labelWidth: 'auto',
          defaultCollapsed: false,
        }}
        onColumnsStateChange={(e) => {
          if (!empty(e)) {
            localStorage.setItem("ContractList现货下单记录列表", JSON.stringify(e))
          }
        }}
        scroll={{x: 1200, y: 666}}
        request={async (params = {}, sort, filter) => {
          // console.log(sort, filter);
          let payload: queryType = {
            page: params.current,
            pageSize: params.pageSize,
          }
          if (params.orderId) {
            payload = {...payload, orderId: params.orderId}
          }
          const msg = await queryOrderStockList(
            payload
          );
          let data = queryUse(msg)
          if (data) {
            return {
              data: data.list,
              // success 请返回 true，
              // 不然 table 会停止解析数据，即使有数据
              success: true,
              // 不传会使用 data 的长度，如果是分页一定要传
              total: data.total,
            };
          } else {
            return {
              data: [],
              success: false,
            };
          }
        }}
        columns={columns}
        pagination={{defaultPageSize:50}}
      />

      </Card>

    </PageContainer>
  );
};

export default StockList;
